package com.cn.designpatterns.ex.concrete.proxy.jdkproxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * this is jdk inner proxy type
 *
 * @author zhangyi
 * @date 2018/12/25 16:00
 */
public class JdkProxy implements InvocationHandler{
    private Object object;

    public JdkProxy(Object object) {
        this.object = object;
    }

    /**
     * 绑定对象
     *
     * @return
     */
    public Object bindObject(){
        return Proxy.newProxyInstance(
                object.getClass().getClassLoader(),
                object.getClass().getInterfaces(),
                this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk 代理之前...");
        Object invoke = method.invoke(object, args);
        System.out.println("jdk 代理之后...");
        return invoke;
    }
}
